package com.toe.user.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.pojo.domain.ToeAuthenticationInfo;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.entity.user.UserFocus;
import com.toe.pojo.entity.user.UserUsertbl;
import com.toe.pojo.vo.user.UserFocusVO;
import com.toe.pojo.vo.user.UserSimpleVO;
import com.toe.pojo.vo.user.UserUserVO;
import com.toe.user.webapi.mapper.UserFocusMapper;
import com.toe.user.service.UserFocusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.user.webapi.mapper.UserUsertblMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户关注用户表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
public class UserFocusServiceImpl extends ServiceImpl<UserFocusMapper, UserFocus> implements UserFocusService {
    @Autowired
    private UserUsertblMapper usertblMapper;
    //用户关注
    @Override
     public Boolean focus(Long focusId){
         //获取当前用户
         Long userId = getUserId();
         if(userId.equals(focusId)){
             throw new ToeServiceException(StateEnum.ERR_BAD_QUERY,"不能关注自己!");
         }
         //查询是否关注过该用户
        QueryWrapper<UserFocus> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("focus_user_id",focusId);
        UserFocus userFocus = getOne(queryWrapper);
        if (userFocus!=null){
            throw new ToeServiceException(StateEnum.ERR_BAD_QUERY,"您已经关注过该用户!");
        }
        //添加关注
         UserFocus uesrFocus=new UserFocus();
         uesrFocus.setUserId(userId);
         uesrFocus.setFocusUserId(focusId);
         boolean focus = save(uesrFocus);
         return focus;
     }

    @Override
    public List<UserFocusVO> getAllFocusUser() {
        Long userId = getUserId();
        UserFocusMapper userFocusMapper = getBaseMapper();
        QueryWrapper<UserFocus> wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        List<UserFocus> userFocusList = userFocusMapper.selectList(wrapper);
        List<UserFocusVO> userFocusVOS=new ArrayList<>();
        //查询被关注的用户信息
        for (UserFocus userfocus:
             userFocusList) {
            UserFocusVO userFocusVO=new UserFocusVO();
            BeanUtils.copyProperties(userfocus,userFocusVO);
            userFocusVOS.add(userFocusVO);
        }
        return userFocusVOS;
    }

    public UserSimpleVO getFocusUserDetail(Long focusId){
        QueryWrapper<UserFocus> wrapperFocus=new QueryWrapper<>();
        wrapperFocus.eq("focus_user_id",focusId);
        UserFocus userFocus = getOne(wrapperFocus);
        if (userFocus==null){
            throw new ToeServiceException(StateEnum.ERR_BAD_QUERY,"您没有关注此用户!");
        }
        UserUsertbl usertbl = usertblMapper.selectById(focusId);
        UserSimpleVO userVO=new UserSimpleVO();
        BeanUtils.copyProperties(usertbl,userVO);
        return userVO;
    }

    //取关
    @Override
    public void cancelFocus(Long focusId){
        QueryWrapper<UserFocus>wrapper=new QueryWrapper();
        wrapper.eq("focus_user_id",focusId);
        remove(wrapper);
    }
    private Long getUserId(){
         Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
         ToeAuthenticationInfo authenticationInfo=(ToeAuthenticationInfo)credentials;
        return authenticationInfo.getId();
     }

}
